package com.soap.desensitize.strategy.rreversible.symmetry;

import com.soap.desensitize.encry.rreversible.symmetry.AbstractSymmetryEncry;
import java.util.Arrays;

public class NumStrategy extends AbstractSymmetryEncry<String> {

    private static NumStrategy instance = null;
    private NumStrategy(){}
    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized NumStrategy getInstance(){
        if(instance == null){
            instance = new NumStrategy();
        }
        return instance;
    }

    @Override
    public String encode(String password, String data) {
        int key = Integer.parseInt(password);
        String[] Numarr= data.split(",");
        int[] arr = Arrays.asList(Numarr).stream().mapToInt(Integer::parseInt).toArray();
        for (int i = 0; i < arr.length; i++) {
            arr[i]=(arr[i]+key)*key;
            //进行数组数据反转
            Numarr[arr.length-1-i]= String.valueOf(arr[i]);
        }
        String result=String.join("-", Numarr);
        return result;
    }

    @Override
    public String decode(String password, String data) {
        int key = Integer.parseInt(password);
        String[] Numarr= data.split("-");
        int[] array = Arrays.asList(Numarr).stream().mapToInt(Integer::parseInt).toArray();
        for (int j = 0; j < array.length; j++) {
            array[j]=array[j]/key-key;
            //转换
            Numarr[array.length-1-j]= String.valueOf(array[j]);
        }
        String result=String.join(",", Numarr);
        return result;
    }

    @Override
    public String encode(String data){
        String[] Numarr= data.split(",");
        int[] arr = Arrays.asList(Numarr).stream().mapToInt(Integer::parseInt).toArray();
        for (int i = 0; i < arr.length; i++) {
            arr[i]=(arr[i]+(i+1))*(i+1);
            //进行数组数据反转
            Numarr[arr.length-1-i]= String.valueOf(arr[i]);
        }
        String result=String.join("-", Numarr);
        return result;
    }

    @Override
    public String decode(String data) {
        String[] Numarr= data.split("-");
        int[] array = Arrays.asList(Numarr).stream().mapToInt(Integer::parseInt).toArray();
        int[] arr1=new int[array.length];
        //转换
        for (int i = 0; i < array.length; i++) {
            arr1[array.length-1-i]=array[i];
        }
        array=arr1;
        for (int j = 0; j < array.length; j++) {
            array[j]=array[j]/(j+1)-(j+1);
            Numarr[j]= String.valueOf(array[j]);
        }
        String result=String.join(",", Numarr);
        return result;
    }
}
